package com.nutiteq.mapper.forms;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;

import com.nutiteq.mapper.MapScreen;
import com.nutiteq.net.DownloadCounter;

public class NetworkInformation extends Form implements CommandListener {
  public static final Command NETWORK_INFO = new Command("Network traffic", Command.ITEM, 0);
  private final Command back;
  private final MapScreen mapScreen;

  public NetworkInformation(final MapScreen mapScreen, final DownloadCounter downloadCounter) {
    super("Network information");
    this.mapScreen = mapScreen;

    append("Download requests: " + Integer.toString(downloadCounter.getNumberOfNetworkRequests()));
    append("\nDownload data: " + Integer.toString(downloadCounter.getDownloadedBytes()) + " bytes");
    append("\nCache hits: " + Integer.toString(downloadCounter.getNumberOfCacheHits()));
    append("\nData from cache: " + Integer.toString(downloadCounter.getBytesLoadedFromCache())
        + " bytes");

    back = new Command("Back", Command.BACK, 0);
    addCommand(back);

    setCommandListener(this);
  }

  public void commandAction(final Command cmd, final Displayable d) {
    if (cmd == back) {
      mapScreen.show(mapScreen);
    }
  }
}
